package com.lightdz.tshirtdz.dal.impl;

import com.lightdz.tshirtdz.model.Enum.ProductTypeEnum;
import com.lightdz.tshirtdz.model.PageInfo;
import com.lightdz.tshirtdz.model.entity.ProductEntity;

import com.lightdz.tshirtdz.utils.MybatisDBHelper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */

@Repository
public class ProductDal {

    /**
     * 根据pid获取商品详情
     *
     * @param pid
     * @return
     */
    public ProductEntity getProduct(long pid) {

        ProductEntity entity =(ProductEntity) MybatisDBHelper.SelectOneById("getProduct", pid);
        return entity;
    }


    public List<ProductEntity> getProductList(ProductTypeEnum productType, PageInfo pageInfo) {

        List<ProductEntity> proList=new ArrayList<>();

        //分页查询
        int offset=0;
        int limit=0;
        if(pageInfo.getPageNo()>1 && pageInfo.getPageSize()>0) {
            offset = (pageInfo.getPageNo()-1) * pageInfo.getPageSize();// offset起始行
        }
        limit = pageInfo.getPageSize(); // limit是当前页显示多少条数据
        RowBounds rowBounds = new RowBounds(offset, limit);

        if(productType==null){
            proList =(List<ProductEntity>) MybatisDBHelper.SelectPageList("getAllProductList",null , rowBounds);
        }
        else {
            ProductEntity entity = new ProductEntity();
            entity.setpType(productType.name());
            proList =(List<ProductEntity>) MybatisDBHelper.SelectPageList("getProductList", entity, rowBounds);
        }

        return proList;

    }

}
